package com.hyz.appalive;

/**
 * Created by dp on 2018/5/17.
 */

import android.annotation.SuppressLint;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.widget.Toast;

/**
 * Created by 86119 on 2017/1/6.
 */

@SuppressLint("NewApi")
public class JobHandlerService extends JobService {
    int n ;
    @Override
    public void onCreate() {
        super.onCreate();
        startJobSheduler();
    }

    public void startJobSheduler() {
        try {
            // 指定你需要执行的JobService
            JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(getPackageName(), JobHandlerService.class.getName()));
            builder.setPeriodic(3000);
            builder.setPersisted(true);
            JobScheduler jobScheduler = (JobScheduler) this.getSystemService(Context.JOB_SCHEDULER_SERVICE);
            jobScheduler.schedule(builder.build());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        Toast.makeText(this , "onStartJob---" + n++ , Toast.LENGTH_SHORT).show();
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        Toast.makeText(this , "onStartJob---00000"  , Toast.LENGTH_SHORT).show();
        return false;
    }

}
